//
//  TicketOrderDetail.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/12/14.
//  Copyright © 2017年 AudioCtrip. All rights reserved.
//

#import "TicketOrderDetail.h"

#import "TicketOrderStateCell.h"
#import "TicketOrderInfor.h"
#import "TicketOrderTicketCell.h"
#import "TicketVideoTipCell.h"
#import "TicketUsingTipCell.h"
#import "ContactCustomerServiceCell.h"
#import "contactsInforCell.h"
#import "TravelPersonsCell.h"
#import "UploadTicketOrderModel.h"
#import "ContactService.h"
#import "TickectDetailVC.h"
#import "TicketPurchaseTipVC.h"
@interface TicketOrderDetail ()<UITableViewDelegate,UITableViewDataSource,TicketOrderTicketCellDelegate>

@property(nonatomic,strong)UITableView * tableView;

@property (nonatomic, strong)UploadTicketOrderModel * uploadOrderModel;

@end

@implementation TicketOrderDetail

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setup];
    [self loadData];
}

- (void)loadData {
    
    [ActivityIndicatorView showHUDAddedTo:self.view];
    [NetAPIManager getTicketOrderRecordDetailWith:self.ticketOrderID block:^(UploadTicketOrderModel *ticketDetailModel, NSError *error) {
        [ActivityIndicatorView hideHUDAddedTo:self.view];
        self.uploadOrderModel = ticketDetailModel;
        [self.tableView reloadData];
    }];
}

- (void)setup {
    
    self.title = MYLocalizedString(@"PAYORDER_TITLE", nil);
    self.view.backgroundColor = kBGColor;
    self.tableView.backgroundColor = kBGColor;
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"TicketOrderStateCell" bundle:nil] forCellReuseIdentifier:@"TicketOrderStateCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TicketOrderInfor" bundle:nil] forCellReuseIdentifier:@"TicketOrderInfor"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TicketOrderTicketCell" bundle:nil] forCellReuseIdentifier:@"TicketOrderTicketCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TicketVideoTipCell" bundle:nil] forCellReuseIdentifier:@"TicketVideoTipCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TicketUsingTipCell" bundle:nil] forCellReuseIdentifier:@"TicketUsingTipCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ContactCustomerServiceCell" bundle:nil] forCellReuseIdentifier:@"ContactCustomerServiceCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"contactsInforCell" bundle:nil] forCellReuseIdentifier:@"contactsInforCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TravelPersonsCell" bundle:nil] forCellReuseIdentifier:@"TravelPersonsCell"];
    
    [self.tableView registerClass:[UITableViewHeaderFooterView  class] forHeaderFooterViewReuseIdentifier:@"headerView"];
}


- (TicketOrderState)getOrderStateWith:(NSInteger)statue {
    
    
    switch (statue) {
        case 16:
        {
            
            return SellTicket_FailState;
        }
            break;
            
        case 0:
        case -1:
        case 2: {
            
             return SellTicket_WaitToPayment;
        }
            break;
        case 15: {
            
            return SellTicket_handling;
        }
            break;
        case 17: {
           return SellTicket_SuccessState;
        }
            break;
        case 3: {
            return SellTicket_Stop;
        }
            break;
        default:
            break;
    }
    
    return SellTicket_handling;
}

#pragma mark -- TicketOrderTicketCellDelegate

- (void)goPurhchaseTipWith:(NSString *)detail {
    
    TicketPurchaseTipVC * tipVC = [[TicketPurchaseTipVC alloc] init];
    
    tipVC.detail = detail;
    
    [self.navigationController  pushViewController:tipVC animated:YES];
}
#pragma mark   -- UITableViewDelegate,UITableViewDataSource


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 2) {
        if ([self getOrderStateWith:self.uploadOrderModel.orderstatus.integerValue] == SellTicket_SuccessState && self.uploadOrderModel.tickets.count == 0) {
            return 0;
        }else if(self.uploadOrderModel.Allpersons.count == 0 && [self getOrderStateWith:self.uploadOrderModel.orderstatus.integerValue] != SellTicket_SuccessState) {
            
            return 0;
        }
    }
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
        case 0:
        {
            TicketOrderStateCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TicketOrderStateCell"];
            cell.superVC = self;
            [cell configDataWithState:self.uploadOrderModel];
            return cell;
        }
            break;
        case 1:
        {
            TicketOrderInfor * cell = [tableView dequeueReusableCellWithIdentifier:@"TicketOrderInfor"];
            
            [cell configDataWith:self.uploadOrderModel];
            
            return cell;
        }
            break;
        case 2:
        {
            if ([self getOrderStateWith:self.uploadOrderModel.orderstatus.integerValue] == SellTicket_SuccessState) {
                
                TicketOrderTicketCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TicketOrderTicketCell"];
                cell.delegate = self;
                [cell configDataWith:self.uploadOrderModel];
                
                return cell;
            }else{
                
                TravelPersonsCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TravelPersonsCell"];
                
                cell.dataSource = self.uploadOrderModel.Allpersons;
                
                return cell;
            }
        }
            break;
        case 3:
        {
            if ([self getOrderStateWith:self.uploadOrderModel.orderstatus.integerValue] == SellTicket_SuccessState) {
                
                TicketVideoTipCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TicketVideoTipCell"];
               cell.tipLabel.text =[NSString stringWithFormat:MYLocalizedString(@"TICKET_SCCESS_COUPON", nil),self.uploadOrderModel.PersonNum];
                return cell;
            }else{
                contactsInforCell * cell = [tableView dequeueReusableCellWithIdentifier:@"contactsInforCell"];
                
                [cell configDataWith:self.uploadOrderModel.contact];
                return cell;
            }
        }
            break;
        case 4:
        {
            if ([self getOrderStateWith:self.uploadOrderModel.orderstatus.integerValue] == SellTicket_SuccessState) {
                TicketUsingTipCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TicketUsingTipCell"];
                [cell configDataWithModel:self.uploadOrderModel];
                return cell;
                
            }else{
                
                ContactCustomerServiceCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ContactCustomerServiceCell"];
                
                return cell;
            }
        }
            break;
        default:
            
            return nil;
            break;
    }
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UITableViewHeaderFooterView * view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"headerView"];
    view.backgroundColor = kBGColor;
    
    return view;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
        case 0:
        {
            
            return [TicketOrderStateCell getCellHeightWithSate:[self getOrderStateWith:self.uploadOrderModel.orderstatus.integerValue]];
        }
            break;
        case 1:
        {
            
            return 187;
        }
            break;
        case 2:
        {
            if ([self getOrderStateWith:self.uploadOrderModel.orderstatus.integerValue] == SellTicket_SuccessState) {
               
                return kScreenWidth - 80;
            }else{
                
                if (self.uploadOrderModel.Allpersons.count > 0) {
                    
                    return (self.uploadOrderModel.Allpersons.count + 1) * 50;
                }
            }
        }
            break;
        case 3:
        {
            if ([self getOrderStateWith:self.uploadOrderModel.orderstatus.integerValue] == SellTicket_SuccessState) {
                
                return 60;
            }else{
                
                return 194;
            }
        }
            break;
        case 4:
        {
            if ([self getOrderStateWith:self.uploadOrderModel.orderstatus.integerValue] == SellTicket_SuccessState) {
                
                if (self.uploadOrderModel.entityticket.boolValue) {
                    return 218;
                }else{
                    return 168;
                }
                
            }else{
                return 60;
            }
        }
            break;
        default:
            
            return 0;
            break;
    }
    
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 2) {
        if ([self getOrderStateWith:self.uploadOrderModel.orderstatus.integerValue] == SellTicket_SuccessState && self.uploadOrderModel.tickets.count == 0) {
            return 0.0001;
        }else if(self.uploadOrderModel.Allpersons.count == 0 && [self getOrderStateWith:self.uploadOrderModel.orderstatus.integerValue] != SellTicket_SuccessState) {
            
            return 0.00001;
        }
    }
    return 10.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    if (section == 4) {
        return 10.f;
    }
    return 0.00001;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if ([cell isKindOfClass:[ContactCustomerServiceCell class]]) {
        
     ContactService *  vc = [IGUtil loadViewController:@"ContactService" storyboard:@"Account"];
        
        [self.navigationController pushViewController:vc animated:YES];
    }else if([cell isKindOfClass:[TicketOrderInfor class]]){
        
        TickectDetailVC * ticketDetailVC = [[TickectDetailVC alloc] init];
        
        ticketDetailVC.hidesBottomBarWhenPushed = YES;
        ticketDetailVC.ticketID = self.uploadOrderModel.unionid;
        [self.navigationController pushViewController:ticketDetailVC animated:YES];
    }else{
        return;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
